package com.ja.openchat.views;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.ja.openchat.R;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.XPopupUtils;

import java.util.List;

public class ChooseListDialog extends BottomPopupView {

    public static void showChooseListDialog(Context context, List<String> mData, int choosePosition, OnChooseListListener onChooseListListener){
        showChooseListDialog(context,mData,choosePosition,null,onChooseListListener);
    }

    public static void showChooseListDialog(Context context, List<String> mData, int choosePosition,String topTitle, OnChooseListListener onChooseListListener){
        new XPopup.Builder(context)
                .asCustom(new ChooseListDialog(context, mData,choosePosition,topTitle,onChooseListListener))
                .show();
    }

    private List<String> mData;
    private OnChooseListListener onChooseListListener;
    private int choosePosition = -1;
    private String topTitle = null;

    public ChooseListDialog(@NonNull Context context, List<String> mData, int choosePosition,String topTitle, OnChooseListListener onChooseListListener) {
        super(context);
        this.topTitle = topTitle;
        this.mData = mData;
        this.onChooseListListener = onChooseListListener;
        this.choosePosition = choosePosition;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        ChooseListAdapter mAdapter = new ChooseListAdapter(mData,choosePosition);
        recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if(onChooseListListener!=null){
                    onChooseListListener.onChoose(position,mAdapter.getItem(position));
                }
                dismiss();
            }
        });
        findViewById(R.id.cancle_btn).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        TextView top_title_text = findViewById(R.id.top_title_text);
        View top_title_line = findViewById(R.id.top_title_line);
        if(!TextUtils.isEmpty(topTitle)){
            top_title_text.setVisibility(View.VISIBLE);
            top_title_line.setVisibility(View.VISIBLE);
            top_title_text.setText(topTitle);
        }else{
            top_title_text.setVisibility(View.GONE);
            top_title_line.setVisibility(View.GONE);
        }
    }

    @Override
    protected int getPopupWidth() {
        return (int) (XPopupUtils.getAppWidth(getContext())*0.9);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_choose_list;
    }

    @Override
    protected int getMaxHeight() {
        return (int) (XPopupUtils.getAppWidth(getContext())*0.8);
    }

    @Override
    public void setForegroundGravity(int foregroundGravity) {
        super.setForegroundGravity(foregroundGravity);
    }
}
